layui.use(['upload'], function () {
    var upload = layui.upload;
    upload.render({
        elem: '#btnUpload1',
        url: '/jzApi/utils/fileImprot',
        size: 1024 * 2048,
        // accept:video,
        exts: 'mp4|webm|ogg',
        timeout: 36000000,
        accept: 'file',
        choose:function(obj){
            //预读本地文件示例，不支持ie8
            layer.load(2);
            obj.preview(function(index, file, result){
                var url = URL.createObjectURL(file);//把file转成URL
                $('#videoattr').attr('src', url); //视频链接
            })},
        done: function (r) {
            $("#enclosure").val(r.data);
            $("#test").html(subFileName(r.data));
                layer.close(layer.index);
                var video_time = document.getElementById("videoattr").duration;//视频时长
                var time_length=Math.ceil(video_time);//转为整型
            $("#videoDuration").val(secondChange(time_length))
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
    upload.render({
        elem: '#btnUpload',
        url: '/jzApi/utils/fileImprot',
        timeout: 36000000,
        size: 1024 * 100,
        accept: 'file',
        // exts: 'docx|doc|xlsx|pdf|ppt',
        done: function (r) {
            $("#enclosure").val(r.data);
            $("#test").html(subFileName(r.data));
        },
        error: function (r) {
            layer.msg(r.msg);
        }
    });
});
// 个位数的话在十位数补0
function formatNumber(n) {
    n = n.toString();
    return n[1] ? n : '0' + n
}
// 秒转为计时器
function secondChange(second) {
    var min=Math.floor(second%3600);
   var secondAfter = formatNumber(Math.floor(second/3600)) + ":" + formatNumber(Math.floor(min/60)) + ":"+ formatNumber(second%60);
   return secondAfter
}
$(function () {
    $("#type").change(function () {
        $("#enclosure").val("");
        $("#test").html("");
        $("#videoDuration").val("");
        var type = $(this).val();
        if (type != '') {
            $("#resourse").show();
            if (type == 1) {  //为视频格式
                $("#btnUpload1").show();
                $("#videoDurationDiv").show();
                $("#btnUpload").hide()
            } else {
                $("#btnUpload1").hide();
                $("#videoDurationDiv").hide();
                $("#btnUpload").show()
            }
        } else {
            $("#resourse").hide();
        }
    });
    $.ajax({
        url: '/jzweb/courses/course',
        success: function (r) {
            var list = r.data;
            var opt = '<option value="">请选择课程</option>';
            for (var i = 0; i < list.length; i++) {
                opt += '<option value="' + list[i].id + '">' + list[i].name + '</option>'
            }
            $("#coursesId").html(opt);
        }
    });

    $("#coursesId").change(function () {
        if ($("#coursesId").val() != "") {
            $("#chapter").css("display", "block");
            $.ajax({
                url: '/jzweb/trainingResources/chapter',
                data: {id: $("#coursesId").val()},
                success: function (r) {
                    var list = r.data;
                    var opt = '<option value="">请选择章节</option>';
                    for (var i = 0; i < list.length; i++) {
                        opt += '<option value="' + list[i].id + '">' + list[i].name + '</option>'
                    }
                    $("#chapterId").html(opt);
                }
            });
        } else {
            $("#chapter").css("display", "none");
        }
    })

});
$().ready(function () {
    validateRule();
});

$.validator.setDefaults({
    submitHandler: function () {
        save();
    }
});

function save() {
    $.ajax({
        cache: true,
        type: "POST",
        url: "/jzweb/trainingResources/save",
        data: $('#signupForm').serialize(),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            coursesId: {
                required: true
            },
            chapterId: {
                required: true
            },
            type: {
                required: true
            },
            fileRoute: {
                required: true
            }
        }
    })
}